unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls;


type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure Panel1Click(Sender: TObject);
    procedure Panel1ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
  end;
const
  N=5;
  M=3;

var
  Form1: TForm1;
  i, j, k: integer;
  arr: array of TPanel;
  min: array of TPanel;
  m1, m2, m3, m4, m5, m6, m7, m8: integer;
  sum, p, o, turn, w: integer;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  randomize;
  turn:=0;
  SetLength(arr, N*N);
  SetLength(min, M);
  for i:=0 to N*N-1 do
  begin
     arr[i]:=TPanel.Create(Form1);
     arr[i].Parent:=Form1;
     arr[i].Width:=18;
     arr[i].Height:=18;
     arr[i].Top:=18*(i div N);
     arr[i].Left:=18*(i mod N);
  end;
  for i:=0 to M-1 do
    min[i]:=arr[Random(N*N-1)];
  for i:= 0 to M-1 do
  begin
    for j:=0 to M-1 do
      if (i<>j) then
        while (min[j]=min[i]) do
          min[j]:=arr[random(N*N-1)];
  end;
  for i:=0 to N*N-1 do
  begin
    arr[i].OnClick:=Panel1Click;
    arr[i].OnContextPopup:=Panel1ContextPopup;
  end
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  randomize;
  for i:=0 to N*N-1 do
  begin
    arr[i].Enabled:=True;
    arr[i].Caption:='';
    arr[i].Color:=clBtnFace;
  end;
  for i:=0 to M-1 do
    min[i]:=arr[Random(N*N-1)];
  for i:= 0 to M-1 do
  begin
    for j:=0 to M-1 do
      if (i<>j) then
        while (min[j]=min[i]) do
          min[j]:=arr[random(N*N-1)];
  end;
  m1:=0;
  m2:=0;
  m3:=0;
  m4:=0;
  m5:=0;
  m6:=0;
  m7:=0;
  m8:=0;
  k:=0;
  p:=0;
  turn:=0;
  w:=0;
end;

procedure TForm1.Panel1Click(Sender: TObject);
begin
  for i:=0 to N*N-1 do
  begin
    if (Sender=arr[i]) then
    begin
      for j:=0 to M-1 do
      begin
        if (arr[i]=min[j]) then
        begin
          k:=1;
          if (arr[i].Color<>clRed) then
          begin
            arr[i].Caption:='M';
            application.MessageBox('You was Booomed', 'Booooom');
            for o:= 0 to N*N-1 do
              arr[o].Enabled:=False;
          end;
        end;
      end;
      if (k<>1) then
      begin
        for j:= 0 to M-1 do
        begin
          if (i mod N <> 0) then
            if(arr[i-1]=min[j]) then
              m1:=1;
          if (i mod N <> N-1) then
            if(arr[i+1]=min[j]) then
              m2:=1;
          if (i div N <> 0) then
            if(arr[i-N]=min[j]) then
              m3:=1;
          if (i div N <> ((N*N-1) div N)) then
            if(arr[i+N]=min[j]) then
              m4:=1;
          if (i mod N<> 0) and (i div N <>0) then
            if(arr[i-N-1]=min[j]) then
              m5:=1;
          if (i mod N<> N-1) and (i div N<>0) then
            if(arr[i-N+1]=min[j]) then
              m6:=1;
          if (i mod N<> 0) and (i div N <> ((N*N-1) div N)) then
            if(arr[i+N-1]=min[j]) then
              m7:=1;
          if (i mod N<> N-1) and (i div N <> ((N*N-1) div N)) then
            if(arr[i+N+1]=min[j]) then
              m8:=1;
        end;
        sum:=m1+m2+m3+m4+m5+m6+m7+m8;
        if (arr[i].Color<>clRed) then
          arr[i].Caption:=inttostr(sum);
        arr[i].Enabled:=False;
        p:=p+1;
      end;
    end;
  end;
  m1:=0;
  m2:=0;
  m3:=0;
  m4:=0;
  m5:=0;
  m6:=0;
  m7:=0;
  m8:=0;
  k:=0;
  if (N*N-M=p) then
  begin
    for o:= 0 to N*N-1 do
      arr[o].Enabled:=False;
    application.MessageBox('You Win', 'winner');
  end;
end;

procedure TForm1.Panel1ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
  for i:=0 to N*N-1 do
  begin
    if (arr[i]=sender) then
    begin
      if (arr[i].Color=clBtnFace) then
      begin
        arr[i].Color:=clRed;
        for j:=0 to M-1 do
        begin
          if (arr[i]=min[j]) then
            turn:=turn+1;
        end;
        w:=w+1;
      end else begin
        arr[i].Color:=clBtnFace;
        for j:=0 to M-1 do
        begin
          if (arr[i]=min[j]) then
            turn:=turn-1;
        end;
        w:=w-1;
      end;
      if (turn=M) and (w=M) then
        application.MessageBox('You Win', 'winner');
    end;
  end;
end;

end.
